using Gateway.Common.Enums;
using Gateway.Common.Extensions;
using Gateway.Domain.Entities;
using Gateway.Domain.SeedModule;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using System.Text.Json.Serialization;
using Gateway.Domain.EFCore.Configuration;

namespace Gateway.Domain.Configuration;

public class MenuConfiguration : AggregateRootEntityConfiguration<Menu>
{
    public override void Configure(EntityTypeBuilder<Menu> builder)
    {
        base.Configure(builder);
        builder.ToTable(t => t.HasComment("菜单/权限表"));
        builder.Property(x => x.Name).IsRequired().HasMaxLength(50).HasComment("菜单code名称");
        builder.Property(x => x.Icon).HasMaxLength(100).HasComment("icon");
        builder.Property(x => x.Title).IsRequired().HasMaxLength(100).HasComment("菜单标题");
        builder.Property(x => x.Path).HasMaxLength(200).HasComment("路由");
        builder.Property(x => x.Component).HasMaxLength(200).HasComment("组件");
        builder.Property(x => x.Tag).HasMaxLength(200).HasComment("标记");
        builder.Property(x => x.Type).IsRequired().HasMaxLength(10).HasComment("类型")
            .HasConversion(x => x.Value, s => MenuTypeEnum.FromValue(s));
        builder.Property(x => x.Hidden).IsRequired().HasMaxLength(1).HasComment("是否隐藏")
            .HasConversion(x => x.Value, s => BooleanStatusEnum.FromValue(s));
        builder.Property(x => x.Affix).IsRequired().HasMaxLength(1).HasComment("是否固定")
            .HasConversion(x => x.Value, s => BooleanStatusEnum.FromValue(s));

        builder.Property(x => x.ParentId).HasMaxLength(36).HasComment("父级标识");
        builder.Property(x => x.SeqNo).HasComment("排序序号");

        builder.Property(x => x.FullPage).IsRequired().HasMaxLength(1).HasComment("整页路由")
            .HasConversion(x => x.Value, s => BooleanStatusEnum.FromValue(s));
        builder.Property(x => x.HiddenBreadcrumb).IsRequired().HasMaxLength(1).HasComment("隐藏面包屑")
            .HasConversion(x => x.Value, s => BooleanStatusEnum.FromValue(s));

        builder.Property(x => x.Active).HasMaxLength(200).HasComment("高亮  子节点或详情页需要高亮的上级菜单路由地址");
        builder.Property(x => x.Redirect).HasMaxLength(200).HasComment("重定向");
        builder.Property(x => x.Color).HasMaxLength(50).HasComment("颜色");
        builder.Property(x => x.LevelCode).HasMaxLength(80).HasComment("层级编码");



        // 初始化 数据
         SetSeed(builder);
    }


    private void SetSeed(EntityTypeBuilder<Menu> builder)
    {
        var dir = AppDomain.CurrentDomain?.BaseDirectory??"";
        Console.WriteLine("dir is :"+dir);

        string path = Path.Combine(dir,"appsettings", "Seed", "menu.json");
        var data = path.ParseJsonFile<List<MenuSeedModel>>();
        List<Menu> menus = new List<Menu>();
        foreach ( var item in data )
        {
            var menu= new Menu(item.Name,item.Path,item.Title,item.Icon,MenuTypeEnum.FromValue(item.Type), item.Component,item.Tag,item.Hidden,item.Affix,
                item.ParentId,item.SeqNo,item.Active,item.Color,item.FullPage,item.Redirect,item.HiddenBreadcrumb,item.LevelCode);
            menu.SetDefaultId(item.MenuId);
            menu.SetCreate("admin", "admin");
            menus.Add(menu);
        }
        builder.HasData(menus);
    }

   

}